package net.huashitong.zhApp.controller;

import com.sirdc.modules.sys.filter.NodeNewschedulesModel;
import com.sirdc.modules.sys.service.PlanTimeReckonService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.responseModel.MailListDataModel;
import net.huashitong.Project.responseModel.NewProjectDataModel;
import net.huashitong.Project.service.ExportService;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.node.service.NodeNewtempletService;
import net.huashitong.user.databean.OnLineDatabean;
import net.huashitong.user.entity.Message;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.filter.MessageFilter;
import net.huashitong.user.filter.OnlineFilter;
import net.huashitong.user.service.LoginLogService;
import net.huashitong.user.service.MessageService;
import net.huashitong.zhApp.service.AppReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigInteger;
import java.text.ParseException;
import java.util.*;

import static net.huashitong.utils.UtilsTools.getCurrentDateTime1;
import static net.huashitong.utils.UtilsTools.getLastMonthDateTime;

@Controller
@RequestMapping(value = "/zhApp/reportManage")
public class AppReportController extends BaseCtrl {
    @Autowired
    private MessageService ms;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private ExportService exportService;
    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Lazy
    @Autowired
    private AppReportService appReportService;
    @Lazy
    @Autowired
    private NodeNewtempletService newtempletService;

    /**
     * 消息报表
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sysMessageList", method = RequestMethod.POST)
    public Map<String, Object> sysMessageList(@RequestBody MessageFilter filter) {
        List<Message> list = ms.getMessageInfo(filter);
        for (Message l : list) {
            if (l.getResDept().contains("业主") || l.getResDept().contains("中介")) {
                NewProjectsFilter newProjectsFilter = new NewProjectsFilter();
                newProjectsFilter.setName(l.getProjectName());
                List<NewProjects> projectInfo = newProjectsService.getProjectInfo(newProjectsFilter);
                if (ObjectUtils.isNotBlank(projectInfo)) {
                    l.setResDept(projectInfo.get(0).getResDept());
                }
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", list);
        result.put("count", filter.getPaging().getRecords());
        return result;
    }

    /**
     * 通讯录
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/reportMailList", method = RequestMethod.POST)
    public Map<String, Object> reportMailList(@RequestBody BnpUserFilter filter) {
        List<MailListDataModel> list = exportService.queryMailListData(filter);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", list);
        result.put("count", filter.getPaging().getRecords());
        return result;
    }

    /**
     * 项目报表
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/projectNodeList", method = RequestMethod.POST)
    public Map<String, Object> projectNodeList(@RequestBody NewProjectsFilter filter) {
        List<NewProjectDataModel> list = exportService.queryProjectListData(filter);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", list);
        result.put("count", filter.getPaging().getRecords());
        return result;
    }

    /**
     * 登录报表
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/signInList", method = RequestMethod.POST)
    public Map<String, Object> signInList(@RequestBody OnlineFilter filter) {
        //默认查询本月
        if (StringUtils.isBlank(filter.getStartDate())) {
            filter.setStartDate(getLastMonthDateTime().replace("-", "."));
        }
        if (StringUtils.isBlank(filter.getEndDate())) {
            filter.setEndDate(getCurrentDateTime1());
        }

        List<OnLineDatabean> list = loginLogService.queryUserOnlineList(filter);
        //统计进行中的节点
        for (OnLineDatabean bean : list) {
            //进行中的节点
            filter.setName(bean.getName());
            BigInteger progressNodes = nodeNewschedulesService.queryProgressNodesCountByFilter(filter);
            bean.setProgressNodes(progressNodes + "");
            //进行中的督查任务数量
            /*List<SupervisionTask> tasks =  supervisionTaskService.getDoing(bean.getName(),bean.getRole());
            BigInteger taskBigInt = new BigInteger(tasks.size()+"");
            bean.setProgressNodes(progressNodes.add(taskBigInt)+"");*/
            bean.setQueryTime(filter.getStartDate() + "--" + filter.getEndDate());
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", list);
        filter.setPaging(null);
        int count = loginLogService.queryUserOnlineListCount();
        result.put("count", count);
        return result;
    }


    /**
     * 节点报表（项目报表）
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryNodeReportDetail", method = RequestMethod.POST)
    public Map<String, Object> queryNodeReportDetail(@RequestBody NodeNewschedulesFilter filter) throws ParseException {
        List<NodeNewschedulesModel> resultNodeModel = appReportService.getNodeDetailByProjectId(filter.getProjectId(), filter.getNodeId());
        resultNodeModel = newtempletService.judgeAddTask(resultNodeModel);
        List<List<String>> levelList = newtempletService.calculateLevel(filter.getProjectId());
        List<NodeNewschedulesModel> finalResult = newtempletService.addLevel(resultNodeModel,levelList);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", finalResult);
        return result;
    }


}
